package com.cy.pj.eiems.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.cy.pj.eiems.Exception.ServiceException;
import com.cy.pj.eiems.dao.SysQuestionDao;
import com.cy.pj.eiems.entity.SysQuestion;
import com.cy.pj.eiems.service.SysQuestionService;
import com.cy.pj.eiems.vo.Node;


@Service
public class SysQuestionServiceImpl implements SysQuestionService{
	@Autowired
	private SysQuestionDao sysQuestionDao;
	/**显示所有的试卷*/
	@Override
	public List<Map<String, Object>> findObjects2() {
		List<Map<String, Object>> list = 
				sysQuestionDao.findObjects2();
		if(list==null||list.size()==0) 
			throw new ServiceException("可能没有试卷了");
		return list;
	}
	
	@Override
	public List<Map<String, Object>> findObjects() {
		List<Map<String, Object>> list = 
				sysQuestionDao.findObjects();
		if(list==null||list.size()==0) 
			throw new ServiceException("试卷为空");
		return list;
	}


	@Override
	public int deleteObject(Integer id) {
		//1.验证数据的合法性
		if(id==null||id<0)              
			throw new IllegalArgumentException("请先选择");
		//3.删除菜单元素
		int rows = sysQuestionDao.deleteObject(id);
		if(rows==0)
			throw new ServiceException("此菜单可能已经不存在");
		//4.删除角色,菜单关系数据
		return rows;
	}


	//ztree
	@Override
	public List<Node> findZtreeExamTypeNodes() {
		return sysQuestionDao.findZtreeExamTypeNodes();
	}




	//保存试卷
	@Override
	public int saveObject(SysQuestion entity) {
		//1.参数校验
		if(entity==null) {
			throw new ServiceException("保存试卷不能为空");
		}
//		if(StringUtils.isEmpty(entity.getContent())) {
//			throw new ServiceException("题目不能为空!");
//		}
		int rows;
		//2.保存数据
		try {
			rows = sysQuestionDao.insertObject(entity);
		} catch (Exception e) {
			e.printStackTrace();
			//Runtime.getRuntime().exec("");//报警,视频,音频
			throw new ServiceException("保存失败");
			//send msg;给运营人员
		}
		//3.返回数据
		return rows;
	}
	
	
	
	
	
	@Override
	public int updateObject(SysQuestion entity) {
		//1.合法验证
		if(entity==null)
			throw new ServiceException("保存对象不能为空");
		if(StringUtils.isEmpty(entity.getContent()))
			throw new ServiceException("题目不能为空");
		//2.更新数据
		int rows=sysQuestionDao.updateObject(entity);
		if(rows==0)
			throw new ServiceException("记录可能已经不存在");
		//3.返回数据
		return rows;
	}	
}
